package cn.bytepulse.web.admin.controller;

import cn.bytepulse.annotation.Log;
import cn.bytepulse.pojo.AjaxResult;
import cn.bytepulse.web.admin.entity.SysUser;
import cn.bytepulse.web.admin.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author jiejiebiezheyang
 * @since 2024-03-11 11:00
 */
@Tag(name = "管理-用户")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/profiles")
    @Operation(summary = "用户信息(权限)")
    @Log(value = "管理-用户-用户信息(权限)", persist = true)
    public AjaxResult profiles() throws Exception {
        return userService.profiles();
    }

    @PostMapping("/avatar")
    @Operation(summary = "上传头像")
    @Log(value = "管理-用户-上传头像", persist = true)
    public AjaxResult avatar(@RequestParam MultipartFile file) throws Exception {
        return userService.avatar(file);
    }

    @GetMapping("/profile")
    @Operation(summary = "用户信息")
    @Log(value = "管理-用户-用户信息", persist = true)
    public AjaxResult profile() throws Exception {
        return userService.profile();
    }

    @PostMapping("/profile/update")
    @Operation(summary = "用户信息更新")
    @Log(value = "管理-用户-用户信息更新", persist = true)
    public AjaxResult profileUpdate(@RequestBody @Validated({SysUser.UpdateProfile.class}) SysUser sysUser) {
        return userService.profileUpdate(sysUser);
    }

    @PostMapping("/passwd")
    @Operation(summary = "修改密码")
    @Log(value = "管理-用户-修改密码", persist = true)
    public AjaxResult passwd(@RequestBody @Validated Passwd passwd) {
        return userService.passwd(passwd);
    }

    @Data
    public static class Passwd {
        @NotBlank(message = "旧密码不能为空")
        private String oldPassword;
        @NotBlank(message = "确认密码不能为空")
        private String confirmPassword;
        @NotBlank(message = "新密码不能为空")
        private String newPassword;
    }
}
